package edu.tyut.game.ui.user.login

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import cn.hutool.crypto.digest.DigestUtil
import edu.tyut.game.GameApplication
import edu.tyut.game.MainActivity
import edu.tyut.game.databinding.ActivityLoginBinding
import edu.tyut.game.logic.model.User
import edu.tyut.game.ui.user.password.ForgetPasswordActivity
import edu.tyut.game.ui.user.password.ForgetPasswordViewModel
import edu.tyut.game.ui.user.register.RegisterActivity
import edu.tyut.game.utils.LogUtils
import edu.tyut.game.utils.StatusBarUtils

class LoginActivity : AppCompatActivity() {

    private val tag = "LoginActivity"

    private lateinit var binding : ActivityLoginBinding

    private val viewModel by lazy {
        ViewModelProvider(this)[LoginViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)
        // 隐藏ActionBar
        // 状态栏透明
        StatusBarUtils.setTranslucentStatusBar(this)

        // 记住密码
        val prefs = getPreferences(Context.MODE_PRIVATE)
        val isRemember = prefs.getBoolean("rememberPassword", false)
        if (isRemember){
            // 将账号密码都设置到文本框中
            val usernameTmp = prefs.getString("username", "")
            val passwordTmp = prefs.getString("password", "")
            binding.username.setText(usernameTmp)
            binding.password.setText(passwordTmp)
            binding.rememberPassword.isChecked = true
        }

        // 用户对象
        var user: User? = null
        viewModel.user.observe(this){
            LogUtils.d(tag, "observe $it")
            if (it.isSuccess){
                // 存全局用户
                GameApplication.loginUser = it.getOrNull()
                val editor = prefs.edit()
                if (binding.rememberPassword.isChecked){
                    editor.putBoolean("rememberPassword", true)
                    editor.putString("username", user?.username)
                    editor.putString("password", user?.password)
                }else{
                    editor.clear()
                }
                editor.apply()
                Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show()
                MainActivity.actionStart(this)
                finish()
            }else{
                Toast.makeText(this, "账号密码不正确", Toast.LENGTH_LONG).show()
            }
        }
        // 登录逻辑
        binding.login.setOnClickListener {
            val usernameTmp = binding.username.text.toString()
            val passwordTmp = binding.password.text.toString()
            val passwordDigest = DigestUtil.sha256Hex(passwordTmp)
            // 去本地数据库查询 ======
            if (usernameTmp.isNotEmpty() and passwordTmp.isNotEmpty()) {
                user = User(0, usernameTmp, passwordTmp, "", "", null, null)
                val userLogin = User(0, usernameTmp, passwordDigest, "", "", null, null)
                viewModel.login(userLogin)
            }else{
                Toast.makeText(this, "请输入用户名密码!", Toast.LENGTH_LONG).show()
            }
        }
        // 注册逻辑
        binding.register.setOnClickListener {
            // 跳到RegisterActivity
            RegisterActivity.actionStart(this)
        }
        // 忘记密码逻辑
        binding.forgetPassword.setOnClickListener {
            ForgetPasswordActivity.actionStart(this)
        }
    }

    // 别的Activity启动LoginActivity
    companion object{
        fun actionStart(context: Context){
            val intent = Intent(context, LoginActivity::class.java)
            context.startActivity(intent)
        }
    }
}
/*
    // 别的Activity启动LoginActivity
    companion object{
        fun actionStart(context: Context, title: String, content: String){
            val intent = Intent(context, LoginActivity::class.java).apply {
                putExtra("news_title", title)
                putExtra("news_content", content)
            }
            context.startActivity(intent)
        }
    }
*/